package pl.edu.mimuw.irs.core.model.analysis.subnet;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import pl.edu.mimuw.irs.core.model.analysis.ResourcePlace;
import pl.edu.mimuw.irs.core.model.analysis.RolePlace;
import pl.edu.mimuw.irs.core.model.design.IRSResourceSet;
import pl.edu.mimuw.irs.core.model.design.processing.Resource;
import pl.edu.mimuw.irs.core.model.design.processing.Role;
import pl.edu.mimuw.irs.core.model.design.processing.RoleInstance;
import pl.edu.mimuw.irs.core.model.petrinet.Net;

public class ResourceSetSubnet extends Net {

	// *** Attributes
	private Set<ResourcePlace> resourcePlaces;
	private Set<RolePlace> rolePlaces;
	private IRSResourceSet resourceSet;
	public Set<ResourcePlace> getResourcePlaces() {
		if (this.resourcePlaces == null) resourcePlaces = new HashSet<ResourcePlace>();
		return resourcePlaces;
	}
	public void setResourcePlaces(Set<ResourcePlace> resourcePlaces) {
		this.resourcePlaces = resourcePlaces;
	}
	public Set<RolePlace> getRolePlaces() {
		if (this.rolePlaces == null) rolePlaces = new HashSet<RolePlace>();
		return rolePlaces;
	}
	public void setRolePlaces(Set<RolePlace> rolePlaces) {
		
		this.rolePlaces = rolePlaces;
	}
	public IRSResourceSet getResourceSet() {
		return resourceSet;
	}
	public void setResourceSet(IRSResourceSet resourceSet) {
		this.resourceSet = resourceSet;
	}
	
	// *** Constructors

	public ResourceSetSubnet(IRSResourceSet resourceSet) {
		setResourceSet(resourceSet);
		Set<ResourcePlace> resourcePlaces = getResourcePlaces();
		Set<RolePlace> rolePlaces = getRolePlaces();
		Map<Role, RolePlace> rolesMap = new HashMap<Role, RolePlace>();
		for (Resource resource : resourceSet.getResources()) {
			for (RoleInstance roleInstance : resource.getRolesInstances()) {
				if (!rolesMap.containsKey(roleInstance)) {
					RolePlace place = new RolePlace(roleInstance.getRole());
					addPlace(place);
					rolesMap.put(roleInstance.getRole(), place);
					rolePlaces.add(place);
				}
			}
			ResourcePlace resourcePlace = new ResourcePlace(resource);
			resourcePlaces.add(resourcePlace);
			addPlace(resourcePlace);
		}
	}
	
	// *** Methods

}
